package com.woniu.inlet.timer;

import com.woniu.adapter.DailyCodeValuationAdapter;
import com.woniu.inlet.web.fo.DataVo;
import com.woniu.outlet.client.PullCodeMsgFeign;
import com.woniu.outlet.mysql.po.DailyCodeValuationMySQL;
import com.woniu.outlet.redis.CodeOneRedisDao;
import com.woniu.outlet.redis.CodeTwoRedisDao;
import com.woniu.outlet.redis.po.DailyCodeTOne;
import com.woniu.outlet.redis.po.DailyCodeTTwo;
import com.woniu.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 拉取code信息
 */
@Component
@Slf4j
public class PullAllCodeMsgTimer {

    @Autowired
    private PullCodeMsgFeign pullCodeMsgFeign;

    @Autowired
    private DailyCodeValuationAdapter dailyCodeValuationAdapter;

    @Autowired
    private CodeOneRedisDao oneRedisDao;

    @Autowired
    private CodeTwoRedisDao twoRedisDao;

    /**
     * "0 0 2 ? * *"
     * 每天2点拉取code信息，昨日的code，存入codeT-2
     */
    @Scheduled(cron = "0 0 2 ? * *")
    public void saveCodeToRedis_T_2() {
        log.debug("【拉取昨日code信息】定时任务开始，当前时间{}",DateUtil.getBuyTime());
        pullFundCodeAndSaveTable();
        pullGoldCodeAndSaveTable();
        List<DailyCodeValuationMySQL> allCode = dailyCodeValuationAdapter.getAllCode();
        //原本表中的code信息，放到redis的codeT-2中
        List<DailyCodeTTwo> t2 = new ArrayList<>();
        for (DailyCodeValuationMySQL mySQL : allCode) {
            DailyCodeTTwo t = new DailyCodeTTwo();
            BeanUtils.copyProperties(mySQL, t);
            t2.add(t);
        }
        twoRedisDao.saveAll(t2);
        log.debug("【拉取昨日code信息】定时任务结束，昨日code存入codeT-2");

    }

    /**
     * "0 15 23 ? * *"
     * 每天23:10点拉取code信息，今日的code,存入codeT-1
     */
    @Scheduled(cron ="0 15 23 ? * *" )
    public void saveCodeToRedis_T_1() {
        log.debug("【拉取今日code信息】定时任务开始，当前时间{}",DateUtil.getBuyTime());
        pullFundCodeAndSaveTable();
        pullGoldCodeAndSaveTable();
        List<DailyCodeValuationMySQL> all = dailyCodeValuationAdapter.getAllCode();
        List<DailyCodeTOne> t1 = new ArrayList<>();
        for (DailyCodeValuationMySQL mySQL : all) {
            DailyCodeTOne t = new DailyCodeTOne();
            BeanUtils.copyProperties(mySQL, t);
            t1.add(t);
        }
        //把拉取code存入表中
        oneRedisDao.saveAll(t1);
        log.debug("【拉取今日code信息】定时任务结束,今日code存入codeT-1");
    }

    /**
     * 拉取黄金code并存入表中
     */
    public void pullGoldCodeAndSaveTable(){
        //拉取所有黄金的code和对应的当日净值
        List<DailyCodeValuationMySQL> allCode = dailyCodeValuationAdapter.getAllCode();
        List<DataVo> allGold = new ArrayList<>();
        while (true){
            try {
                allGold=pullCodeMsgFeign.getAllGold();
                break;
            } catch (Exception e) {
                log.debug("feign拉取gold失败，重试");
            }
        }
        log.debug("feign拉取gold成功");
        String pullDate = DateUtil.getSendDate();
        if (allCode.size() > 0) {
            StringBuilder sb = new StringBuilder();
            for (DailyCodeValuationMySQL mySQL : allCode) {
                String code = mySQL.getCodeId() + ",";
                sb.append(code);
            }
            int count=0;
            //执行更新操作
            for (DataVo v : allGold) {
                DailyCodeValuationMySQL codeValuationMySQL = DailyCodeValuationMySQL.builder()
                        .codeId(v.getCode_id())
                        .name(v.getName())
                        .dayGrowth(v.getDay_growth()+"%")
                        .todayValuation(v.getToday_valuation())
                        .pullDate(pullDate)
                        .build();
                //存在的code更新,不存在则插入
                if (sb.indexOf(v.getCode_id()) != -1){
                    dailyCodeValuationAdapter.updateTodayValuationByCodeId(codeValuationMySQL);
                }else{
                    dailyCodeValuationAdapter.saveCode(codeValuationMySQL);
                    count++;
                }
            }
            log.debug("黄金更新成功,新加入的code数量[{}]个",count);
        } else {
            //插入操作
            for (DataVo v : allGold) {
                DailyCodeValuationMySQL codeValuationMySQL = DailyCodeValuationMySQL.builder()
                        .codeId(v.getCode_id())
                        .name(v.getName())
                        .dayGrowth(v.getDay_growth()+"%")
                        .todayValuation(v.getToday_valuation())
                        .pullDate(pullDate)
                        .build();
                dailyCodeValuationAdapter.saveCode(codeValuationMySQL);
            }
            log.debug("黄金插入成功");
        }
    }



    /**
     * 拉取基金code并存入表中
     */
    public void pullFundCodeAndSaveTable(){
        //拉取所有的基金的code和对应的当日净值
        List<DailyCodeValuationMySQL> allCode = dailyCodeValuationAdapter.getAllCode();
        List<DataVo> all = new ArrayList<>();
        while (true){
            try {
                all=pullCodeMsgFeign.getAll();
                break;
            } catch (Exception e) {
                log.debug("feign拉取fund失败，重试");
            }
        }
        log.debug("feign拉取fund成功");
        String pullDate = DateUtil.getSendDate();
        if (allCode.size() > 0) {
            StringBuilder sb = new StringBuilder();
            for (DailyCodeValuationMySQL mySQL : allCode) {
                String code = mySQL.getCodeId() + ",";
                sb.append(code);
            }
            int count=0;
            //执行更新操作
            for (DataVo v : all) {
                System.out.println(v.toString());
                DailyCodeValuationMySQL codeValuationMySQL = DailyCodeValuationMySQL.builder()
                        .codeId(v.getCode_id())
                        .name(v.getName())
                        .dayGrowth(v.getDay_growth()+"%")
                        .todayValuation(v.getToday_valuation())
                        .pullDate(pullDate)
                        .build();
                //存在的code更新,不存在则插入
                if (sb.indexOf(v.getCode_id()) != -1){
                    dailyCodeValuationAdapter.updateTodayValuationByCodeId(codeValuationMySQL);
                }else{
                    dailyCodeValuationAdapter.saveCode(codeValuationMySQL);
                    count++;
                }
            }
            log.debug("基金更新成功,新加入的code数量[{}]个",count);
        } else {
            //插入操作
            for (DataVo v : all) {
                DailyCodeValuationMySQL codeValuationMySQL = DailyCodeValuationMySQL.builder()
                        .codeId(v.getCode_id())
                        .name(v.getName())
                        .dayGrowth(v.getDay_growth()+"%")
                        .todayValuation(v.getToday_valuation())
                        .pullDate(pullDate)
                        .build();
                dailyCodeValuationAdapter.saveCode(codeValuationMySQL);
            }
            log.debug("基金插入成功");
        }
    }
}
